﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DungeonDelver.Common
{
    public class BoardColumn<T,U>:IBoardColumn<T,U>
    {
        public IBoard<T, U> Board
        {
            get;
            set;
        }

        public int Column
        {
            get 
            {
                foreach (var column in Board.Columns)
                {
                    if (Board[column] == this)
                    {
                        return column;
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }

        private Dictionary<int, IBoardCell<T, U>> rows = new Dictionary<int, IBoardCell<T, U>>();

        public IBoardCell<T, U> this[int theIndex]
        {
            get
            {
                if (rows.ContainsKey(theIndex))
                {
                    return rows[theIndex];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (value == null)
                {
                    if (rows.ContainsKey(theIndex))
                    {
                        rows[theIndex].BoardColumn = null;
                        rows.Remove(theIndex);
                    }
                }
                else
                {
                    if (rows.ContainsKey(theIndex))
                    {
                        rows[theIndex].BoardColumn = null;
                        rows[theIndex] = value;
                    }
                    else
                    {
                        rows.Add(theIndex, value);
                    }
                    rows[theIndex].BoardColumn = this;
                }
            }
        }

        public IEnumerable<int> Rows
        {
            get { return rows.Keys; }
        }
    }
}
